package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GamePriceTemplate;
import com.sls.web.manager.game.querybean.GamePriceTemplateQuery;
import com.sls.web.manager.game.service.GamePriceTemplateService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 价格模板Impl
 * 
 * @author gesx
 * 
 */
@Repository("gamePriceTemplateService")
public class GamePriceTemplateServiceImpl extends SuperService implements
		GamePriceTemplateService {

	private static Logger log = Logger
			.getLogger(GamePriceTemplateServiceImpl.class);
	private Gson gson;

	public GamePriceTemplateServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(GamePriceTemplate gamePriceTemplate,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GamePriceTemplateServiceImpl save method .....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_PRICE_TEMPLATE_SAVE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gamePriceTemplate.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gamePriceTemplate);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GamePriceTemplateServiceImpl save method ..... ");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(GamePriceTemplate gamePriceTemplate,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GamePriceTemplateServiceImpl save method .....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_PRICE_TEMPLATE_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gamePriceTemplate.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gamePriceTemplate);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GamePriceTemplateServiceImpl save method ..... ");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/*
	 * @Override public boolean delete(GamePriceTemplate gamePriceTemplate,
	 * UserInfoVO userInfoVO) throws Exception { log.info("当前用户：" +
	 * userInfoVO.getUserId() +
	 * ",Enter GamePriceTemplateServiceImpl save method ....."); Head head = new
	 * Head(); head.setRequestId(UUID.randomUUID().toString());
	 * head.setServiceCode(Constants.GAME_PRICE_TEMPLATE_SAVE);
	 * head.setIp(userInfoVO.getCurrentIp());
	 * head.setUserId(userInfoVO.getUserId()); SocketDTO dto = super.send(head,
	 * gamePriceTemplate); if (dto != null && dto.getHead() != null &&
	 * TransmissionCode.SUCCESS.getCode().equals(
	 * dto.getHead().getResponseCode())) { log.info("服务器返回信息：" + dto.getBody());
	 * log.info("当前用户：" + userInfoVO.getUserId() +
	 * ",Leave GamePriceTemplateServiceImpl save method ..... "); return true; }
	 * else { throw new ManagerException(dto.getHead().getResponseMessage()); }
	 * }
	 */

	@Override
	public Pagination<GamePriceTemplate> queryList(
			GamePriceTemplateQuery gamePriceTemplateQuery, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter GamePriceTemplateServiceImpl queryList method .....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_PRICE_TEMPLATE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, gamePriceTemplateQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			String body = dto.getBody();
			Pagination<GamePriceTemplate> game = gson.fromJson(body,
					new TypeToken<Pagination<GamePriceTemplate>>() {
					}.getType());
			return game;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GamePriceTemplate queryByCode(String code, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GamePriceTemplateServiceImpl save method .....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_PRICE_TEMPLATE_BY_CODE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GamePriceTemplate game = new GamePriceTemplate();
		game.setTemplateCode(code);
		SocketDTO dto = super.send(head, game);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			GamePriceTemplate game2 = gson.fromJson(dto.getBody(),
					GamePriceTemplate.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GamePriceTemplateServiceImpl save method ..... ");
			return game2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<String> queryTemplateNameList(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_TEMPLATE_NAME);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			List<String> templateNames = gson.fromJson(body,
					new TypeToken<List<String>>() {
					}.getType());
			return templateNames;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GamePriceTemplate> queryTemplateList(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021027");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			List<GamePriceTemplate> templates = gson.fromJson(body,
					new TypeToken<List<GamePriceTemplate>>() {
					}.getType());
			return templates;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}



}
